//
//  SuperviseTaskIssueVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//
// 【首页-检查监督】检查任务-报告列表

import Foundation
import UIKit

class SuperviseTaskIssueVC: SubLevelViewController {
    let tableView = UITableView(frame: .zero, style: .plain)
    
    var datas: [SpRwbgtzModal?] = []
    
    var id: Int64!
    
    init(id: Int64!) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navBarStyle(.white)
        view.backgroundColor = .white
        title = "问题列表"
        setupUI()
        reloadData()
    }
    
    func reloadData() {
        API.getJcjdRwbgtz(withParam: SpDetailParam(rwjlid: id)) { responseModel in
            DispatchQueue.main.async {
                print("responseModel", responseModel)
                self.datas = responseModel.models ?? []
                self.tableView.tableShowEmpty(withDataCount: self.datas.count)
                self.tableView.reloadData()
            }
        }
    }
    
    // MARK: - Setup
    
    func setupUI() {
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .white
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}

extension SuperviseTaskIssueVC: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = datas[indexPath.row]
        let ID : String = "SuperviseTaskIssueCell"
            
        var cell = tableView.dequeueReusableCell(withIdentifier: ID)
        if cell == nil {
            cell = SuperviseTaskIssueCell(style: .subtitle, reuseIdentifier: ID)
        }
        
        (cell as? SuperviseTaskIssueCell)?.reload(withItem: data)
        return cell!
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 72
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let data = datas[indexPath.row]
        navigationController?.pushViewController(SuperviseTaskIssueDetailVC(id: data?.id!), animated: true)
    }
    
}
